﻿using System;
using System.Collections.Generic;

using System.Text;
using RebEngine.Managers;
using TomShane.Neoforce.Controls;
using Microsoft.Xna.Framework.Graphics;
using RebellionII.GameObjects.Units;
using RebellionII.GameObjects.Map;

namespace RebellionII.GUI
{
    public class ProductionWindow : Window
    {
        #region Window
        public int SelectedReference{ get; set; }
        private List<int> Selectables;
        private RebSystem system;
        #endregion Window

        private Label lblSelectedName;
        private ImageBox imgSelectedObject;
        private GroupPanel costBox;
        private Label lblCost;
        private ImageBox imgCost;
        private Button button5;
        private Button button2;
        private ComboBox txtBoxQuantityToBuild;
        private Label ulblQuantity;
        private Panel panel1;
        private Label label6;
        private Label label5;
        private Label label4;
        private Label label3;
        private ComboBox cmbSelections;
        private ImageBox imgBacking;
        private GroupPanel upkeepBox;
        private Label lblUpkeep;
        private ImageBox imgUpkeep;

        public ProductionWindow(List<int> selectableUnits, RebSystem inSys):
            base(InputManager.Manager)
        {
            system = inSys;
            SelectedReference = selectableUnits[0];
            Selectables = selectableUnits;
            Init();
            Left = 50;
            Top = 50;
            Height = 365;
            Width = 225;
            Resizable = false;
            string type = NetClientManager.UnitList[selectableUnits[0]].GetType().Name.ToString();
            Name = inSys.sName + " " + type + " Production Dialog";
            Text = inSys.sName + " " + type + " Production Dialog";
            InputManager.Manager.Add(this);
            this.Closing += new WindowClosingEventHandler(ProductionWindow_Closing);

            imgBacking = new ImageBox(InputManager.Manager);
            imgBacking.Init();
            imgBacking.Left = 5;
            imgBacking.Top = 5;
            imgBacking.Width = 200;
            imgBacking.Height = 100;
            imgBacking.Text = "";
            imgBacking.Image = TextureManager.GetTexture("BackingStars").BaseTexture as Texture2D;
            imgBacking.SizeMode = SizeMode.Stretched;
            imgBacking.SendToBack();
            Add(imgBacking);

            imgSelectedObject = new ImageBox(InputManager.Manager);
            imgSelectedObject.Init();
            imgSelectedObject.Left = 20;
            imgSelectedObject.Top = 15;
            imgSelectedObject.Width = 170;
            imgSelectedObject.Height = 78;
            imgSelectedObject.Text = "";
            imgSelectedObject.Image = TextureManager.GetTexture(NetClientManager.UnitList[SelectedReference].sTextureName).BaseTexture as Texture2D;
            imgSelectedObject.SizeMode = SizeMode.Stretched;
            Add(imgSelectedObject);

            lblSelectedName = new Label(InputManager.Manager);
            lblSelectedName.Init();
            lblSelectedName.Left = 70;
            lblSelectedName.Top = 110;
            lblSelectedName.Width = 130;
            lblSelectedName.Height = 13;
            lblSelectedName.Text = NetClientManager.UnitList[SelectedReference].sName;
            Add(lblSelectedName);

            cmbSelections = new ComboBox(InputManager.Manager);
            cmbSelections.Init();
            cmbSelections.Left = 25;
            cmbSelections.Top = 130;
            cmbSelections.Width = 165;
            cmbSelections.Height = 21;

            foreach (var temp in selectableUnits)
            {
                cmbSelections.Items.Add(NetClientManager.UnitList[temp].sName);
            }
            cmbSelections.ItemIndex = 0;
            cmbSelections.ItemIndexChanged += new TomShane.Neoforce.Controls.EventHandler(cmbSelections_ItemIndexChanged);
            Add(cmbSelections);

            costBox = new GroupPanel(InputManager.Manager);
            costBox.Init();
            costBox.Left = 110;
            costBox.Top = 155;
            costBox.Width = 100;
            costBox.Height = 64;
            costBox.Text = "Cost";
            Add(costBox);

            lblCost = new Label(InputManager.Manager);
            lblCost.Init();
            lblCost.Left = 65;
            lblCost.Top = 13;
            lblCost.Width = 35;
            lblCost.Height = 13;
            lblCost.Text = NetClientManager.UnitList[SelectedReference].iConstructionCost.ToString();
            costBox.Add(lblCost);

            imgCost = new ImageBox(InputManager.Manager);
            imgCost.Init();
            imgCost.Left = 10;
            imgCost.Top = 5;
            imgCost.Width = 30;
            imgCost.Height = 30;
            imgCost.Text = "";
            imgCost.Image = TextureManager.GetTexture("Cost").BaseTexture as Texture2D;
            imgCost.SizeMode = SizeMode.Stretched;
            costBox.Add(imgCost);

            button5 = new Button(InputManager.Manager);
            button5.Init();
            button5.Left = 10;
            button5.Top = 300;
            button5.Width = 90;
            button5.Height = 25;
            button5.Text = "Build";
            Add(button5);
            button5.Click += new TomShane.Neoforce.Controls.EventHandler(BuildSelected);

            button2 = new Button(InputManager.Manager);
            button2.Init();
            button2.Left = 115;
            button2.Top = 300;
            button2.Width = 90;
            button2.Height = 25;
            button2.Text = "Close";
            Add(button2);
            button2.Click += new TomShane.Neoforce.Controls.EventHandler(CloseWindow);

            txtBoxQuantityToBuild = new ComboBox(InputManager.Manager);
            txtBoxQuantityToBuild.Init();
            txtBoxQuantityToBuild.Left = 150;
            txtBoxQuantityToBuild.Top = 278;
            txtBoxQuantityToBuild.Width = 40;
            txtBoxQuantityToBuild.Height = 20;
            txtBoxQuantityToBuild.Text = "1";
            if(NetClientManager.UnitList[SelectedReference] is Facility)
            {
                for (int i = 0; i < system.iNumFacilities - system.lFacilities.Count; i++)
                {
                     txtBoxQuantityToBuild.Items.Add(i+1);
                }
            }
            else
            {
                for (int i = 0; i < 20; i++)
                {
                    txtBoxQuantityToBuild.Items.Add(i+1);
                }
            }
            Add(txtBoxQuantityToBuild);

            ulblQuantity = new Label(InputManager.Manager);
            ulblQuantity.Init();
            ulblQuantity.Left = 20;
            ulblQuantity.Top = 280;
            ulblQuantity.Width = 140;
            ulblQuantity.Height = 13;
            ulblQuantity.Text = "Quantity to Build:";
            Add(ulblQuantity);

            panel1 = new Panel(InputManager.Manager);
            panel1.Init();
            panel1.Left = 5;
            panel1.Top = 222;
            panel1.Width = 206;
            panel1.Height = 53;
            panel1.Text = "";
            Add(panel1);

            label6 = new Label(InputManager.Manager);
            label6.Init();
            label6.Left = 190;
            label6.Top = 10;
            label6.Width = 20;
            label6.Height = 13;
            label6.Text = "0";
            panel1.Add(label6);

            label5 = new Label(InputManager.Manager);
            label5.Init();
            label5.Left = 190;
            label5.Top = 30;
            label5.Width = 13;
            label5.Height = 13;
            label5.Text = "0";
            panel1.Add(label5);

            label4 = new Label(InputManager.Manager);
            label4.Init();
            label4.Left = 5;
            label4.Top = 30;
            label4.Width = 170;
            label4.Height = 13;
            label4.Text = "Time to Deployment:";
            panel1.Add(label4);

            label3 = new Label(InputManager.Manager);
            label3.Init();
            label3.Left = 5;
            label3.Top = 10;
            label3.Width = 170;
            label3.Height = 13;
            label3.Text = "Time to Finish Construction:";
            panel1.Add(label3);

            
            upkeepBox = new GroupPanel(InputManager.Manager);
            upkeepBox.Init();
            upkeepBox.Left = 5;
            upkeepBox.Top = 155;
            upkeepBox.Width = 100;
            upkeepBox.Height = 64;
            upkeepBox.Text = "Upkeep";
            Add(upkeepBox);

            lblUpkeep = new Label(InputManager.Manager);
            lblUpkeep.Init();
            lblUpkeep.Left = 65;
            lblUpkeep.Top = 10;
            lblUpkeep.Width = 20;
            lblUpkeep.Height = 20;
            lblUpkeep.Text = NetClientManager.UnitList[SelectedReference].iMaintenanceCost.ToString();
            upkeepBox.Add(lblUpkeep);

            imgUpkeep = new ImageBox(InputManager.Manager);
            imgUpkeep.Init();
            imgUpkeep.Left = 10;
            imgUpkeep.Top = 5;
            imgUpkeep.Width = 30;
            imgUpkeep.Height = 30;
            imgUpkeep.Text = "";
            imgUpkeep.Image = TextureManager.GetTexture("Upkeep").BaseTexture as Texture2D;
            imgUpkeep.SizeMode = SizeMode.Stretched;
            upkeepBox.Add(imgUpkeep);
        }

        void ProductionWindow_Closing(object sender, WindowClosingEventArgs e)
        {
            InputManager.Manager.Remove(this);
        }

        void BuildSelected(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            if (NetClientManager.UnitList[SelectedReference] is Facility)
            {
                system.ClearFacilityBuild();
                system.BuildFacilties(SelectedReference, Convert.ToInt32(txtBoxQuantityToBuild.Text), NetClientManager.UnitList[SelectedReference].iConstructionCost);
            }
            if (NetClientManager.UnitList[SelectedReference] is Troop)
            {
                system.ClearTroopBuild();
                system.BuildTroops(SelectedReference, Convert.ToInt32(txtBoxQuantityToBuild.Text), NetClientManager.UnitList[SelectedReference].iConstructionCost);
            }
            if (NetClientManager.UnitList[SelectedReference] is CapitalShip)
            {
                system.ClearShipBuild();
                system.BuildShips(SelectedReference, Convert.ToInt32(txtBoxQuantityToBuild.Text), NetClientManager.UnitList[SelectedReference].iConstructionCost);
            }
            if (NetClientManager.UnitList[SelectedReference] is Fighter)
            {
                system.ClearShipBuild();
                system.BuildShips(SelectedReference, Convert.ToInt32(txtBoxQuantityToBuild.Text), NetClientManager.UnitList[SelectedReference].iConstructionCost);
            }
            foreach (var temp in InputManager.Manager.Controls)
            {
                if (temp is BuildWindow)
                    (temp as BuildWindow).RefreshData();
            }
            InputManager.Manager.Remove(this);
            Close();
        }

        void CloseWindow(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            InputManager.Manager.Remove(this);
            Close();
            
        }

        void cmbSelections_ItemIndexChanged(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            SelectedReference = Selectables[cmbSelections.ItemIndex];
            imgSelectedObject.Image = TextureManager.GetTexture(NetClientManager.UnitList[SelectedReference].sTextureName).BaseTexture as Texture2D;
            lblSelectedName.Text = NetClientManager.UnitList[SelectedReference].sName;

            lblCost.Text = NetClientManager.UnitList[SelectedReference].iConstructionCost.ToString();
            lblUpkeep.Text = NetClientManager.UnitList[SelectedReference].iMaintenanceCost.ToString();

            txtBoxQuantityToBuild.Items.Clear();
            if (NetClientManager.UnitList[SelectedReference] is Facility)
            {
                if (((NetClientManager.UnitList[SelectedReference] as Facility).eType == FacilityType.Resources))
                {
                    int numRes = 0;
                    foreach (var temp in system.lFacilities)
                    {
                        if ((NetClientManager.UnitList[temp.iReferenceID] as Facility).eType == FacilityType.Resources)
                            numRes++;
                    }
                    for (int i = 0; i < ((system.iNumFacilities - system.lFacilities.Count) < system.iNumEnergy - numRes ? (system.iNumFacilities - system.lFacilities.Count) : system.iNumEnergy - numRes); i++)
                    {
                        txtBoxQuantityToBuild.Items.Add(i + 1);
                    }
                }
                else
                {
                    for (int i = 0; i < system.iNumFacilities - system.lFacilities.Count; i++)
                    {
                        txtBoxQuantityToBuild.Items.Add(i + 1);
                    }
                }
            }
            else
            {
                for (int i = 0; i < 20; i++)
                {
                    txtBoxQuantityToBuild.Items.Add(i + 1);
                }
            }
        }
    }
}
